﻿
using System;
using System.Data;
using System.Collections.Generic;

namespace LDSP.BRL
{
    public partial class EnterpriseUser : BYFW.BOL.BaseEntity
    {
        public EnterpriseUser()
        {
            m_tableName = "t_EnterpriseUser";
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected string m_code = string.Empty;
        protected string m_password = string.Empty;
        protected EnterpriseUserType m_userType = EnterpriseUserType.企业管理员;
        protected int m_enterpriseID = 0;
        protected Enterprise m_enterprise = null;
        protected bool m_isValid = true;

        /// <summary>
        /// 密码
        /// </summary>
        public string Code
        {
            get { return m_code; }
            set { m_code = value; }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return m_password; }
            set { m_password = value; }
        }
        /// <summary>
        /// 企业用户类型
        /// </summary>
        public EnterpriseUserType UserType
        {
            get { return m_userType; }
            set { m_userType = value; }
        }
        /// <summary>
        /// 企业ID
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
            set { m_enterpriseID = value; }
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid
        {
            get { return m_isValid; }
            set { m_isValid = value; }
        }

        public Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (m_enterpriseID != 0)
                    {
                        m_enterprise.GetProperties(m_enterpriseID);
                    }
                }
                return m_enterprise;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("code", m_code);
            m_container.Add("password", m_password);
            m_container.Add("userType", (int)m_userType);
            m_container.Add("enterpriseID", m_enterpriseID);
            m_container.Add("isValid", m_isValid ? 1 : 0);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_code = this.GetStringValue(record, "code");
            m_password = this.GetStringValue(record, "password");
            m_userType = (EnterpriseUserType)this.GetIntValue(record, "userType");
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            m_isValid = this.GetBoolValue(record, "isValid");
            return true;
        }

        public bool CanUpdate(string transID)
        {
            string condition = string.Format(
           "WHERE {0}={1} AND {2}<>{3} and {4}={5}"
           , DB.GetCol("code"), DB.GetVal(m_code)
           , DB.GetCol(m_idField), DB.GetVal(m_id)
           , DB.GetCol("enterpriseid"), DB.GetVal(m_enterpriseID));
            return !this.Exist(transID, condition, false);
        }

        public bool GetPropertiesByEnterpriseID(string code, int enterpriseID)
        {
            string cond = string.Format("where {0}={1} and {2}={3}", DB.GetCol("code"),DB.GetVal(code),DB.GetCol("enterpriseID"),DB.GetVal(enterpriseID));
            DataTable dt = this.GetRecords(cond);
            return this.GetProperties(dt.Rows.Count > 0 ? dt.Rows[0] : null);
        }

        public override int Delete(string transID)
        {
            new R_CodificationUser().DeleteByUser(m_id);
            return base.Delete(transID);
        }
    }
}